package com.meisiabc.support.wechat;

import com.riversoft.weixin.app.base.AppSetting;
import com.riversoft.weixin.app.user.SessionKey;
import com.riversoft.weixin.app.user.Users;
import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;

@Slf4j
public class WechatClient {
    private AppSetting appSetting;

    public WechatClient() {
        this.appSetting = new AppSetting("wxd10308eaab66e9b4", "8a0bacb35ff56f979f39316d9d95766e");
    }

    public WechatSession code2Session(String code) {
        try {
            SessionKey sessionKey = Users.with(this.appSetting).code2Session(code);
            if (sessionKey != null) {
                log.info("code2Session success.[code={},openid={}]", code, sessionKey.getOpenId());
                return WechatSession.builder().openid(sessionKey.getOpenId()).sessionKey(sessionKey.getSessionKey()).build();
            }
        } catch (Exception e) {
            log.error("code2Session exception.[code={}]", code, e);
        }
        return null;
    }

    public InputStream qrCodes(String path, String scene) {
        try {
            return WechatQrCodes.with(this.appSetting).create(path, scene);
        } catch (Exception e) {
            log.error("qrCodes exception.[path={},scene={}]", path, scene, e);
        }
        return null;
    }
}
